package com.ruoyi.system.utils;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.CsvData;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.math.BigDecimal;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class CsvUtils {

    public static void InsertValueToCSV(String filePath){
        try {
            // 读取原始 CSV 文件内容
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            StringBuilder sb = new StringBuilder();
            String line;
            int lineNumber = 0;
            //修改头部数据
            String head = reader.readLine();
            line = "id"+head;
            sb.append(line).append("\n");
            lineNumber++;
            while ((line = reader.readLine()) != null) {
                sb.append(line).append("\n");
                lineNumber++;
            }
            reader.close();

            // 将修改后的 CSV 数据写入文件
            BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
            writer.write(sb.toString());
            writer.close();

            System.out.println("First line modified successfully.");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static List<CsvData> parseCSVFile(String filePath) {
        try (BufferedReader reader = new BufferedReader(new FileReader(RuoYiConfig.getProfile()+filePath.replace("profile","")));
             CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT.withFirstRecordAsHeader())) {
            List<CsvData> list=new LinkedList<>();
            Long userId = SecurityUtils.getUserId();
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (CSVRecord csvRecord : csvParser) {
                CsvData csvData=new CsvData();
                //long转Date
                csvData.setTimeStamp(dateFormat.parse(csvRecord.get("bob")));
                csvData.setStartPrice(new BigDecimal(csvRecord.get("open")));
                csvData.setMaxPrice(new BigDecimal(csvRecord.get("high")));
                csvData.setMinPrice(new BigDecimal(csvRecord.get("low")));
                csvData.setEndPrice(new BigDecimal(csvRecord.get("close")));
                csvData.setTurnover(Long.valueOf(csvRecord.get("volume")));
                csvData.setUserId(userId);
                csvData.setCreateTime(date);
                csvData.setUpdateTime(date);
                list.add(csvData);
            }
            return list;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }


}
